package atm;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 登录面板
 */
public class LoginPanel extends JPanel implements ActionListener {
    private ATMFrame atmFrame;
    private ImageIcon loginImg = new ImageIcon("images/buttonLand.jpg");
    private JLabel accountLabel = new JLabel("账号：");
    private JLabel pwdLabel = new JLabel("密码：");
    private JTextField accountField = new JTextField();
    private JPasswordField pwdField = new JPasswordField();
    private JButton loginBtn = new JButton(loginImg);
    private Font font = new Font("黑体",Font.BOLD,30);
    private int inputCount;

    public LoginPanel(ATMFrame atmFrame){
        this.atmFrame = atmFrame;
        this.setLayout(null);

        accountLabel.setBounds(300,200,120,50);
        accountLabel.setFont(font);

        accountField.setBounds(400,200,250,50);
        accountField.setFont(font);

        pwdLabel.setBounds(300,260,120,50);
        pwdLabel.setFont(font);

        pwdField.setBounds(400,260,250,50);
        pwdField.setFont(font);

        loginBtn.setBounds(440,400,140,50);
        this.add(accountLabel);
        this.add(accountField);
        this.add(pwdLabel);
        this.add(pwdField);
        this.add(loginBtn);
        loginBtn.addActionListener(this);
        // 设置当前面板的背景为透明
        this.setOpaque(false);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == loginBtn){
            String account = accountField.getText();
            String pwd = new String (pwdField.getPassword());
            SuperArray users = atmFrame.getUsers();
            for (int i = 0; i < users.size();i++){
                User user = (User) users.get(i);
                if(user.getAccount().equals(account)
                        && user.getPwd().equals(pwd)){
                    atmFrame.setLoginUser(user);
                    atmFrame.switchPanel("menu");
                    return;
                }
            }
            inputCount++;
            JOptionPane.showMessageDialog(this,"账号或密码错误，还剩"+(3 - inputCount)+"次");
            if(inputCount >= 3){
                JOptionPane.showMessageDialog(null,"次数用户，卡被吞");
                System.exit(0);
            }
            }
        }
    }

